// 通用函数
import useClipboard from 'vue-clipboard3';
import { ElMessage } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import { useI18n } from 'vue-i18n';

export default function () {
	const { t } = useI18n();
	const { toClipboard } = useClipboard();

	// 百分比格式化
	const percentFormat = (row: EmptyArrayType, column: number, cellValue: string) => {
		return cellValue ? `${cellValue}%` : '-';
	};
	// 列表日期时间格式化
	const dateFormatYMD = (row: EmptyArrayType, column: number, cellValue: string) => {
		if (!cellValue) return '-';
		return formatDate(new Date(cellValue), 'YYYY-mm-dd');
	};
	// 列表日期时间格式化
	const dateFormatYMDHMS = (row: EmptyArrayType, column: number, cellValue: string) => {
		if (!cellValue) return '-';
		return formatDate(new Date(cellValue), 'YYYY-mm-dd HH:MM:SS');
	};
	// 列表日期时间格式化
	const dateFormatHMS = (row: EmptyArrayType, column: number, cellValue: string) => {
		if (!cellValue) return '-';
		let time = 0;
		if (typeof row === 'number') time = row;
		if (typeof cellValue === 'number') time = cellValue;
		return formatDate(new Date(time * 1000), 'HH:MM:SS');
	};
	// 小数格式化
	const scaleFormat = (value: string = '0', scale: number = 4) => {
		return Number.parseFloat(value).toFixed(scale);
	};
	// 小数格式化
	const scale2Format = (value: string = '0') => {
		return Number.parseFloat(value).toFixed(2);
	};
	// 点击复制文本
	const copyText = (text: string) => {
		return new Promise((resolve, reject) => {
			try {
				//复制
				toClipboard(text);
				//下面可以设置复制成功的提示框等操作
				ElMessage.success(t('message.layout.copyTextSuccess'));
				resolve(text);
			} catch (e) {
				//复制失败
				ElMessage.error(t('message.layout.copyTextError'));
				reject(e);
			}
		});
	};
	type CallbackFunction = (...args: any[]) => void;

	// 防抖函数
	const debounce = <T extends (...args: any[]) => void>(callback: T, delay: number = 300): ((...args: Parameters<T>) => void) => {
		let timeoutId: ReturnType<typeof setTimeout>;
		return function (this: any, ...args: Parameters<T>): void {
			clearTimeout(timeoutId);
			timeoutId = setTimeout(() => {
				callback.apply(this, args);
			}, delay);
		};
	};

	// 节流函数
	const throttle = <T extends (...args: any[]) => void>(callback: T, delay: number = 300): T => {
		let lastExecTime = 0;
		let timer: ReturnType<typeof setTimeout> | null = null;

		return function (this: any, ...args: Parameters<T>): void {
			const currentTime = new Date().getTime();

			if (!lastExecTime || currentTime - lastExecTime >= delay) {
				callback.apply(this, args);
				lastExecTime = currentTime;
			} else if (!timer) {
				timer = setTimeout(
					() => {
						callback.apply(this, args);
						lastExecTime = new Date().getTime();
						timer = null;
					},
					delay - (currentTime - lastExecTime)
				);
			}
		} as T;
	};

	// 节流 + 防抖
	const debounceThrottle = (fn: CallbackFunction, delay: number = 5000): ((event: any) => void) => {
		let startTime: number = 0;
		let timer: ReturnType<typeof setTimeout> | null = null;
		return function (this: any, event: any): void {
			const endTime: number = new Date().getTime();
			if (timer !== null) {
				clearTimeout(timer);
			}
			if (endTime - startTime > delay) {
				fn.apply(this, arguments as any);
				startTime = new Date().getTime();
			} else {
				timer = setTimeout(
					() => {
						fn.apply(this, [event]);
					},
					delay - (endTime - startTime)
				);
			}
		};
	};

	return {
		percentFormat,
		dateFormatYMD,
		dateFormatYMDHMS,
		dateFormatHMS,
		scaleFormat,
		scale2Format,
		copyText,
		debounce,
		throttle,
		debounceThrottle,
	};
}
